package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.HospitalSubscribeMedicalExaminerInfo;
import com.ruoyi.system.domain.HospitalSubscribeOrder;
import com.ruoyi.system.service.IHospitalSubscribeMedicalExaminerInfoService;
import com.ruoyi.system.service.IHospitalSubscribeOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HospitalSubscribeInspectionResultsMapper;
import com.ruoyi.system.domain.HospitalSubscribeInspectionResults;
import com.ruoyi.system.service.IHospitalSubscribeInspectionResultsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 检查项目结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-26
 */
@Service
public class HospitalSubscribeInspectionResultsServiceImpl implements IHospitalSubscribeInspectionResultsService
{
    @Autowired
    private HospitalSubscribeInspectionResultsMapper hospitalSubscribeInspectionResultsMapper;

    @Autowired
    private IHospitalSubscribeOrderService iHospitalSubscribeOrderService;

    @Autowired
    private IHospitalSubscribeMedicalExaminerInfoService iHospitalSubscribeMedicalExaminerInfoService;

    /**
     * 查询检查项目结果
     * 
     * @param id 检查项目结果主键
     * @return 检查项目结果
     */
    @Override
    public HospitalSubscribeInspectionResults selectHospitalSubscribeInspectionResultsById(String id)
    {
        return hospitalSubscribeInspectionResultsMapper.selectHospitalSubscribeInspectionResultsById(id);
    }

    /**
     * 查询检查项目结果列表
     * 
     * @param hospitalSubscribeInspectionResults 检查项目结果
     * @return 检查项目结果
     */
    @Override
    public List<HospitalSubscribeInspectionResults> selectHospitalSubscribeInspectionResultsList(HospitalSubscribeInspectionResults hospitalSubscribeInspectionResults)
    {
        return hospitalSubscribeInspectionResultsMapper.selectHospitalSubscribeInspectionResultsList(hospitalSubscribeInspectionResults).stream().peek(item->{
            HospitalSubscribeOrder order = iHospitalSubscribeOrderService.getOrderInfoByOrderNo(item.getOrderNo());
            Long examinerInfoId = order.getExaminerInfoId();
            HospitalSubscribeMedicalExaminerInfo hospitalSubscribeMedicalExaminerInfo = iHospitalSubscribeMedicalExaminerInfoService.selectHospitalSubscribeMedicalExaminerInfoById(examinerInfoId);
            BeanUtils.copyProperties(hospitalSubscribeMedicalExaminerInfo,item);
        }).collect(Collectors.toList());
    }

    /**
     * 新增检查项目结果
     * 
     * @param hospitalSubscribeInspectionResults 检查项目结果
     * @return 结果
     */
    @Override
    public int insertHospitalSubscribeInspectionResults(HospitalSubscribeInspectionResults hospitalSubscribeInspectionResults)
    {
        hospitalSubscribeInspectionResults.setCreatedTime(new Date());
        return hospitalSubscribeInspectionResultsMapper.insertHospitalSubscribeInspectionResults(hospitalSubscribeInspectionResults);
    }

    /**
     * 修改检查项目结果
     * 
     * @param hospitalSubscribeInspectionResults 检查项目结果
     * @return 结果
     */
    @Override
    public int updateHospitalSubscribeInspectionResults(HospitalSubscribeInspectionResults hospitalSubscribeInspectionResults)
    {
        return hospitalSubscribeInspectionResultsMapper.updateHospitalSubscribeInspectionResults(hospitalSubscribeInspectionResults);
    }

    /**
     * 批量删除检查项目结果
     * 
     * @param ids 需要删除的检查项目结果主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeInspectionResultsByIds(String[] ids)
    {
        return hospitalSubscribeInspectionResultsMapper.deleteHospitalSubscribeInspectionResultsByIds(ids);
    }

    /**
     * 删除检查项目结果信息
     * 
     * @param id 检查项目结果主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeInspectionResultsById(String id)
    {
        return hospitalSubscribeInspectionResultsMapper.deleteHospitalSubscribeInspectionResultsById(id);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int insertHospitalSubscribeInspectionResultsList(List<HospitalSubscribeInspectionResults> hospitalSubscribeInspectionResults) {
        for (HospitalSubscribeInspectionResults hospitalSubscribeInspectionResult : hospitalSubscribeInspectionResults) {
            this.insertHospitalSubscribeInspectionResults(hospitalSubscribeInspectionResult);
        }
        iHospitalSubscribeOrderService.completeOrder(hospitalSubscribeInspectionResults.stream().map(HospitalSubscribeInspectionResults::getOrderId).collect(Collectors.toList()).get(0));
        return 1;
    }
}
